/**
 * 
 */
package com.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.mapper.SysRoleMenuMapper;
import com.blog.pojo.SysRole;
import com.blog.pojo.SysRoleMenu;
import com.blog.pojo.SysRoleMenuExample;
import com.blog.service.SysRoleMenuService;

/**
 * @author zyj
 *
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
	
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	/* 
	 * ���õ�ǰ��ɫ��ԴȨ��
	 */
	@Override
	public void setMenus(SysRole role, long[] menuIds) {
		//ɾ����ǰ��ɫ���е�Ȩ��
		SysRoleMenuExample example= new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(role.getId());
        List<SysRoleMenu> rms= sysRoleMenuMapper.selectByExample(example);
        for (SysRoleMenu roleMenu : rms)
        	sysRoleMenuMapper.deleteByPrimaryKey(roleMenu.getId());
 
        //�����µ�Ȩ�޹�ϵ
        if(null!=menuIds)
            for (long mid : menuIds) {
            	SysRoleMenu sysRoleMenu = new SysRoleMenu();
            	sysRoleMenu.setMenuId(mid);
            	sysRoleMenu.setRoleId(role.getId());
            	sysRoleMenuMapper.insert(sysRoleMenu);
            }
	}

	/* 
	 * ɾ����ɫ
	 */
	@Override
	public void deleteByRole(long roleId) {
		SysRoleMenuExample example= new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<SysRoleMenu> rms= sysRoleMenuMapper.selectByExample(example);
        for (SysRoleMenu roleMenu : rms)
        	sysRoleMenuMapper.deleteByPrimaryKey(roleMenu.getId());
	}

	/* 
	 * ɾ����Դ
	 */
	@Override
	public void deleteByMenu(long menuId) {
		SysRoleMenuExample example= new SysRoleMenuExample();
        example.createCriteria().andMenuIdEqualTo(menuId);
        List<SysRoleMenu> rms= sysRoleMenuMapper.selectByExample(example);
        for (SysRoleMenu roleMenu : rms)
            sysRoleMenuMapper.deleteByPrimaryKey(roleMenu.getId());

	}

}
